package com.haiziwang.yst.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * 用于压缩和解压gzip格式的数据，减少请求中的数据传输量
 * @author luoh
 */
public class GzipUtil {

    private static final Logger logger = LoggerFactory.getLogger(GzipUtil.class);

    public static String unzip(String base) {
        BASE64Decoder decoder = new BASE64Decoder();
        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        GZIPInputStream gzipInputStream = null;
        String result = "";
        try {
            byte[] bytes = decoder.decodeBuffer(base);
            out = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(bytes);
            gzipInputStream = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = gzipInputStream.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            result = out.toString("UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("gzip uncompress error.", e);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
                if (gzipInputStream != null) {
                    gzipInputStream.close();
                }
            } catch (IOException e) {
                //ignore
            }
        }
        return result;
    }


    public static String gzip(String content) {
        BASE64Encoder base64 = new BASE64Encoder();
        ByteArrayOutputStream out = null;
        try {
            out = new ByteArrayOutputStream();
            GZIPOutputStream encoder = new GZIPOutputStream(out);
            encoder.write(content.getBytes("UTF-8"));
            //必须先关闭gzip流，out才能获取完整数据
            encoder.close();
            return base64.encodeBuffer(out.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    public static void main(String[] args) {
        String s="H4sIAAAAAAAAAO2dTYtcRRSG/4r0SiGEOnXrfmWXcRJwkazciYvOdBMHMzOhp7MIIrhyggtRCAHB\n" +
                "pYJbxY2E5NckGfMv7B4zMZyTXb+XesE3u+l7u+n0Q9Wt85xTpz77ajZ/sDhc35off3yyWM6uzWxM\n" +
                "qeu7PLvy9srt+dH2you/Hr96cvby7JfNpTsPDu8tNpfeffnDy7d+9M4N8VMvr7z3U08Pj5enp58s\n" +
                "ZtfG1DRXZgdfzFd3l7dOjpcPZ9fyOI5Xx+Hy1U8f3t9+QNq88eDkeL2aH6xvPzi6s1xtXszJujQm\n" +
                "S6m1zfXF8v76zTcZUtp+i+XxYn++Xv57a5/aZnvb3ZOTxenlN97cVzavHZ7uL9fzw3vbO7d/rpdH\n" +
                "b+9ot68czd/83Vln2y+zWh68+91Wy6P56svL6x+UVz8/evH02flPz16f/Xj+6PcXT5+cP//+/I/n\n" +
                "L8++/fuHX18//ubVn09ffvfb5p2nh3e3P/DNcmPvuu3f6HN/vdj10fbafUt9W/Zu7t3s2n5753q+\n" +
                "Wvv/z9dXmOEWB7cZywZun97+a3bmXMS5PufWcW7TdhD3O8PtBLc+3M7P0AaZoc0Etz7c3sG9mKDH\n" +
                "ndlqViZgO4SnL4RtK7b12Y6ObcGw1QO3PtuSphm3g9jWZ2vTsB3Ftj7b7NlC1lI5iW19tl5TDRi2\n" +
                "ioEI2EZLBWErA0nA1pupDjNuFd8SsPViCjQnK74lYOu9lGUMXAW4BHC9mGoxD9xebOuz9WIKI5Sz\n" +
                "5EV9tq0XU6BMUJa9IIDrzVQPGbiNIlwCtt5MgYpsGoW4BHC9msI8cRuFuARsvZoaISvlRiEuAVuv\n" +
                "ppoGMykrxCWA692UGWbkKsYlgOvlVIdhqxiXgK13U127XU51O8NVjEsA18spzMAtytDXZ9tNUzVV\n" +
                "5C8I2IaqqVQQS+Uif0EA18spw+ToSyO49eF6OYWpUy6SUwRsvZwCxbhFdooAbrBTmElZcoqArZdT\n" +
                "oAeu3BQBW++mMLU1RW6KgK13U/1FjLuzmypyUwRwvZuyFjIrt5JT9eH2Xk5hnrit5BQBWy+nQONW\n" +
                "boqAbXBTAyRH38pNEcD1bgpT8djKTRGw9W4KNClLTRGwnWZPXys1RcDWq6kLe/Ffv7hkO2OWpSLA\n" +
                "7C1VxiR0W2kqArheUxlmt0ErTUUA12sqzKbrTpaqPtvBWyrD1FB1inYJ4HpNNWDYKtolYOs1Faau\n" +
                "tVO0S8DWWypMIUanMIiArbdUGJPRaaFMwDb0RIew7ZXyI2DrLRWmyKZXyo+Abeg8hQmCei2UCeB6\n" +
                "NZUxbJU6IGAbCqhAcLVSrg939GoKs49kkHYkYOvNlCUMXC2VCeB6NVUuqmx2Phpq0FqZAG7Y3QeJ\n" +
                "cQflCwjYht19mCzuIKlMANfLKdBySuUXBGyn2d03aqlMwDbKKQxcLZUJ4Ia6qYSBqycuAVxvpyDp\n" +
                "oM1rYlub7ebXDa2nwMdcW9IETcB5khbppqPqGdiGFukZEe5akssggOs9FaQ+brM8E9v6bL2nghwU\n" +
                "ZUkpXQK2oYZqQDTj3fwuglsfrvdUkHS9JTlIAraTdKGypMJWArZRU0HgmkJcArihiArDViFufbbm\n" +
                "NRVmUjZFuARsvZrCRLimhTIB29CECsNWC2UCtt5MYdJCpoUyAVtvpiDVcZaV8iNgG9qjQ+bkrBiI\n" +
                "gK0XU5hhq2QBAVrvpTAZ3KwKGwK23kuViw4nu+40sKxMEAFc76VAz1vFQPXZ5lA+hZmUFQMRsA17\n" +
                "+wwCt1EQRADXi6nGEF08rVEURAA3dEeHPHEbZYII2L5nbx+CrTJBBGy9mUJ30LZG0S4B5klaUFmj\n" +
                "YJeAbdjlB2mybI2yuQRwvabCZOobmQwCtqE5OoRtUbBbn23jLRWmXLko1iVg6y1VbiEioyggIoAb\n" +
                "OlBBFlNF+VwCttNs7CuKcAnYhvIpDFsFQQRsJ2mObjqlnoFtKJ/CsFU6l4CtN1MjhK3OqGdgG3qj\n" +
                "Y4IgHVLPANebKYy90CH1BGxLbD8FYSt5QcA2nNqHeeAqwCVgG1pOYdgqwCVgO0lndNP51gxsvZjC\n" +
                "bCLR8dYMbGNjdABanW7NgDZ4qRZxEol1ioEI4HoxBWl6bzq5nIGtF1OYqhqdXM7AdhovpZPLCdi2\n" +
                "3kthkvOdSpQJ2E7TCl2n0jOw9V4KtJaSuyBg671U7iFt7jvJCwK4XkxhkgW97AUBWy+mMIdDWa/s\n" +
                "PAHccGQfZKXcy0wRsPVmClPG2stMEbANrdAxbGWmCNiGTuiY563MVH22XdjL10KioF5qigCuV1Pw\n" +
                "Bhi9TAYB5lA91UHKlXuZDAK4XlNhFlaDTAYBW2+pMOXKg0QGAdvQfAqyaB4kMgjYekmFadM6KNgl\n" +
                "YDuNpBoU7BKwDX3RMWwV6xKwDW3RMXOyYqD6bPtwXB+E7agYiICtd1SY7UGjYiACtuG4PsykPCoI\n" +
                "IoA7zXl9o7K5BGxDV3RM4eOoCJcA7uRt0UcFuwSY49l9kCGsYJeAbWiLjtksNGrTAQFcb6lAi2al\n" +
                "6gnYeks1YsatLFV9tsMkzadykqUiYOstFeSMmc0lsa3PdpKu6DlJUhGw9ZIKHevmJKVBgNn7Kkix\n" +
                "TU7yGARsp9ZVOUlpEGD2ugqyYzcnGQ0CtqFXOnwIS24QYA57ADFDWHKDgK0XV5gA2CQ36rMdwx7A\n" +
                "BrEHMJvsBgHc0J4KM3BlNwjYho1/GTNwVYNDADds/IOspky+ioBtPNAPgVa6igCt11WQyoxsclQE\n" +
                "bL2jwmhmk6MiYBtKqjDpe5OZIoAbSqowbGWmCNhO0jc9Z5mp6mxzCmYK8sDNElMEbCfpm56zxBQB\n" +
                "20n6pucsL0XANvRNxzxv5aUI2EYvBWErMUXAdvI6qixHRYDZOypIv8Cc5agI2Iaj/ZJdtbIz2/+L\n" +
                "ovr8H6/aYbL/RQEA\n";
//        s="啊哈";
//        s = gzip(s+s+s+s+s+s);
//        System.out.println(s);
        System.out.println(unzip(s));
    }
}
